﻿using System;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.PowerPoint;
using Task = System.Threading.Tasks.Task;
using WordApplication = Microsoft.Office.Interop.Word.Application;
using ExcelApplication = Microsoft.Office.Interop.Excel.Application;
using PptApplication = Microsoft.Office.Interop.PowerPoint.Application;
using System.IO;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;


//namespace OfficeScript
//{
class Startup
{
    public async Task<object> DllApplication(dynamic options)
    {
        return await Task.Run(() => new
        {
            wordToPdf = (Func<object, Task<object>>)(async (dynamic input) => await Task.Run(() => this.wordToPdf((string)input.sourcePath, (string)input.targetPath))),
            excelToPdf = (Func<object, Task<object>>)(async (dynamic input) => await Task.Run(() => this.excelToPdf((string)input.sourcePath, (string)input.targetPath))),
            pptToPdf = (Func<object, Task<object>>)(async (dynamic input) => await Task.Run(() => this.pptToPdf((string)input.sourcePath, (string)input.targetPath))),
        });
    }

    public string wordToPdf(string sourcePath, string targetPath)
    {
        WordApplication wordApp = new WordApplication();
        wordApp.Visible = false;
        try
        {
            Document wordDoc = wordApp.Documents.Open(sourcePath);
            wordDoc.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF);
            wordDoc.Close(WdSaveOptions.wdDoNotSaveChanges);
        }
        finally
        {
            wordApp.Quit();
        }
        return "word转pdf：" + sourcePath;
    }

    public string excelToPdf(string sourcePath, string targetPath)
    {
        ExcelApplication excelApp = new ExcelApplication();
        excelApp.Visible = false;
        try
        {
            Workbook workbook = excelApp.Workbooks.Open(Path.GetFullPath(sourcePath));
            workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, targetPath, XlFixedFormatQuality.xlQualityStandard, IncludeDocProperties: false, IgnorePrintAreas: false);
            workbook.Close();
        }
        finally
        {
            excelApp.Quit();
        }
        return "excel转pdf：" + sourcePath;
    }

    public string pptToPdf(string sourcePath, string targetPath)
    {
        PptApplication pptApp = new PptApplication();
        pptApp.Visible = MsoTriState.msoFalse;
        try
        {
            Presentation presentation = pptApp.Presentations.Open(sourcePath);
            presentation.ExportAsFixedFormat(targetPath, PpFixedFormatType.ppFixedFormatTypePDF);
            presentation.Close();
        }
        finally
        {
            pptApp.Quit();
        }
        return "ppt转pdf：" + sourcePath;
    }

}



//}
